package com.ktjiaoyu.controller;

import com.ktjiaoyu.pojo.Vehiclecomplaints;
import com.ktjiaoyu.service.VehiclecomplaintsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VehiclecomplaintsController {
    @Resource
    VehiclecomplaintsService vehiclecomplaintsService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Long brandId, Model model){
        //调用service 进行查询
        List<Vehiclecomplaints> list = vehiclecomplaintsService.selectAll(brandId);
        //将数据存储到model
        model.addAttribute("list",list);

        //条件进行回显
        model.addAttribute("brandId",brandId);

        //返回视图
        return "index";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping("/save")
    public String save(Vehiclecomplaints vehiclecomplaints){
        int rs = vehiclecomplaintsService.insert(vehiclecomplaints);

        if(rs>0){
            return "redirect:/list";
        }else{
            return "redirect:/add";
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Vehiclecomplaints vehiclecomplaints){
        Map<String,Object> map=new HashMap<>();
        int rs = vehiclecomplaintsService.update(vehiclecomplaints);
        if(rs>0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }
        return map;
    }
}
